from flask_admin import Admin
from flask_admin.menu import MenuLink
from flask_login import current_user

from src.admin_panel.app import db
from src.admin_panel.app.constants import logs_path, media_path
from src.admin_panel.app.views import (
    ActivationCodeModelView,
    AdminUserBaseModelview,
    BillModelView,
    CalendarModelView,
    CalendarTaskModelView,
    CategoryModelView,
    CheckMessageModelView,
    MyFileAdmin,
    SettingsModelView,
    SubscriptionModelView,
    UserCalendarModelView,
    UserModelView,
)
from src.admin_panel.app.views.index import MyAdminIndexView
from src.database.models import (
    ActivationCode,
    AdminUser,
    Bill,
    BotSettings,
    Calendar,
    CalendarTask,
    Category,
    CheckMessage,
    Subscription,
    User,
    UserCalendar,
)


class LogoutMenuLink(MenuLink):
    def is_accessible(self):
        return current_user.is_authenticated


def init_admin_panel(app):
    admin = Admin(
        app,
        name="Admin Dashboard",
        base_template="myadmin3/my_master.html",
        template_mode="bootstrap4",
        index_view=MyAdminIndexView(url="/"),
    )

    admin.add_view(
        UserModelView(
            User,
            db.session,
            name="Пользователи",
            menu_icon_type="fas",
            menu_icon_value="fa-user",
            endpoint="users",
        )
    )
    admin.add_view(
        CategoryModelView(
            Category,
            db.session,
            name="Категории",
            menu_icon_type="fas",
            menu_icon_value="fa-list",
            endpoint="categories",
        )
    )
    admin.add_view(
        CalendarModelView(
            Calendar,
            db.session,
            name="Календари",
            menu_icon_type="fas",
            menu_icon_value="fa-cubes",
            endpoint="calendars",
        )
    )
    admin.add_view(
        UserCalendarModelView(
            UserCalendar,
            db.session,
            name="Календари пользователей",
            menu_icon_type="fas",
            menu_icon_value="fa-user",
            endpoint="user-calendars",
        )
    )
    admin.add_view(
        CalendarTaskModelView(
            CalendarTask,
            db.session,
            name="Задачи календарей",
            menu_icon_type="fas",
            menu_icon_value="fa-tasks",
            endpoint="tasks",
        )
    )
    admin.add_view(
        CheckMessageModelView(
            CheckMessage,
            db.session,
            name="Проверочные сообщения",
            menu_icon_type="fas",
            menu_icon_value="fa-envelope",
            endpoint="check_messages",
        )
    )
    admin.add_view(
        ActivationCodeModelView(
            ActivationCode,
            db.session,
            name="Коды активации",
            menu_icon_type="fas",
            menu_icon_value="fa-percent",
            endpoint="activation_codes",
        )
    )
    admin.add_view(
        SubscriptionModelView(
            Subscription,
            db.session,
            name="Подписки",
            menu_icon_type="fas",
            menu_icon_value="fa-repeat",
            endpoint="subscriptions",
        )
    )
    admin.add_view(
        BillModelView(
            Bill,
            db.session,
            name="Счета пользователям",
            menu_icon_type="fas",
            menu_icon_value="fa-money",
            endpoint="bills",
        )
    )
    admin.add_view(
        SettingsModelView(
            BotSettings,
            db.session,
            name="Настройки бота",
            menu_icon_type="fas",
            menu_icon_value="fa-gears",
            endpoint="bot_settings",
        )
    )
    admin.add_view(
        AdminUserBaseModelview(
            AdminUser,
            db.session,
            name="Администраторы",
            menu_icon_type="fas",
            menu_icon_value="fa-users",
            endpoint="admin-user",
        )
    )
    admin.add_view(
        MyFileAdmin(
            media_path,
            name="Загруженные файлы",
            menu_icon_type="fas",
            menu_icon_value="fa-copy",
        )
    )
    admin.add_view(
        MyFileAdmin(
            logs_path,
            name="Логи",
            menu_icon_type="fas",
            menu_icon_value="fa-copy",
            endpoint="logs",
        )
    )
